
using System;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 用于替代原有的SelectHandler, 该脚本作为组件挂在对象上使用，需配合ChooseEntity
/// @foldcc
/// </summary>
public class ChooseController : MonoBehaviour
{

    [SerializeField, Header("自动获取所有选项")]
    public bool IsAutoRefresh;
    [SerializeField, Header("刷新后默认选中第一个")]
    public bool IsAutoFirstSelect;
    [SerializeField, Header("是否设置为只读状态（只读状态下无法对其item做操作）")]
    public bool IsReadonlyHandler;

    private int _mCurChooseId = -1;
    protected Dictionary<int, ChooseEntity> mChooseDict;
    public Dictionary<int, ChooseEntity> ChooseDict
    {
        get => mChooseDict;
    }
    public Action<ChooseEntity> OnSelect;

    public ChooseEntity CurChoose
    {
        get
        {
            if (this._mCurChooseId != -1)
            {
                return this.mChooseDict[this._mCurChooseId];
            }
            return null;
        }
    }
    private void Start()
    {
        if (this.IsAutoRefresh)
        {
            this.RefreshChoose();
        }
    }

    /// <summary>
    /// 手动重置指定ChooseEntity
    /// </summary>
    /// <param name="ChooseEntities"></param>
    public void RefreshChoose<T>(List<T> ChooseEntities) where T : ChooseEntity
    {
        this._mCurChooseId = -1;
        if (this.mChooseDict != null)
        {
            this.mChooseDict.Clear();
        }
        else
        {
            this.mChooseDict = new Dictionary<int, ChooseEntity>();
        }
        int count = 0;
        foreach (var rChooseEntity in ChooseEntities)
        {
            this.mChooseDict[count] = rChooseEntity;
            rChooseEntity.InitChoose(count++, this.ActiveChoose);
        }

        if (this.IsAutoFirstSelect)
        {
            this.ActiveChoose(0);
        }
    }

    /// <summary>
    /// 取消所有选择
    /// </summary>
    public void UnSelectAll()
    {
        foreach (ChooseEntity Entity in this.mChooseDict.Values)
        {
            Entity.ChangeState(false);
        }
    }

    /// <summary>
    /// 获取一个Choose对象
    /// </summary>
    /// <param name="id">Choose ID</param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public T GetChoose<T>(int id)
    where T : ChooseEntity
    {
        if (this.mChooseDict.ContainsKey(id))
        {
            return (T)this.mChooseDict[id];
        }
        return null;
    }

    /// <summary>
    /// 自动重置所有Choose，获取该节点下的所有ChooseEntity
    /// </summary>
    public virtual void RefreshChoose()
    {
        this.RefreshChoose(new List<ChooseEntity>(this.GetComponentsInChildren<ChooseEntity>()));
    }

    /// <summary>
    /// 激活指定ID的选项
    /// </summary>
    /// <param name="ID"></param>
    public void ActiveChoose(int ID)
    {
        if (this.SelectChoose(ID))
        {
            this.OnSelect?.Invoke(this.mChooseDict[ID]);
        }
    }

    /// <summary>
    /// 仅选中指定ID的Choose，不会触发OnSelect
    /// </summary>
    /// <param name="ID"></param>
    /// <returns></returns>
    public bool SelectChoose(int ID)
    {
        if (this.IsReadonlyHandler)
        {
            return false;
        }
        if (this.mChooseDict.Count > 0 && this.mChooseDict.ContainsKey(ID))
        {
            this._mCurChooseId = ID;
            foreach (var Value in this.mChooseDict.Values)
            {
                Value.ChangeState(Value.ChooseID == ID);
            }
            return true;
        }
        return false;
    }
}